package com.strawcom.charmring.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.strawcom.charmring.App;
import com.strawcom.charmring.BaseFragment;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.bean.DailySummary;
import com.strawcom.charmring.bean.WeeklySummary;
import com.strawcom.charmring.bean.WeeklySummary.WeekSummary;
import com.strawcom.charmring.bluetooth.RingBleService;
import com.strawcom.charmring.util.FormatUtil;
import com.strawcom.charmring.view.ChartView;
import com.strawcom.charmring.view.HorizontalWheelView;
import com.strawcom.charmring.view.HorizontalWheelView.OnItemSelectedListener;

/**
 * @author WangPeng
 * 
 */
public class ExerciseWeekSummaryFragment extends BaseFragment {

	private WeeklySummary weeklySummary;
	private final List<WeekSummary> weekSummaryList = new ArrayList<WeekSummary>();

	private int selectedIndex = 0;

	private HorizontalWheelView tabs;
	private ChartView viewChart;

	@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_exercise_week_summary, container, false);

		tabs = (HorizontalWheelView) view.findViewById(R.id.pagerSlidingTabStrip);
		tabs.setOnItemSelectedListener(onItemSelectedListener);

		viewChart = (ChartView) view.findViewById(R.id.view_chart);

		initExerciseSummaryInfo();

		return view;
	}

	@Override public void onResume() {
		super.onResume();
		refreshSummaryInfo();
	}
	
	@Override public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(!hidden) refreshSummaryInfo();
	}

	@Override public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		registerRingMessageReceiver();
	}

	@Override public void onDestroy() {
		super.onDestroy();
		unregisterRingMessageReceiver();
	}

	private void refreshSummaryInfo() {
		boolean refresh = true;
		if (weekSummaryList.size() > 0) {
			List<DailySummary> dsList = weekSummaryList.get(weekSummaryList.size() - 1).dailySummaryList;
			if (dsList.get(dsList.size() - 1).getDate().equals(App.i().getDailySummary().getDate())) {
				refresh = false;
			}
		}

		if (refresh) {
			weeklySummary.handleSummaryInfo();
			weekSummaryList.clear();
			weekSummaryList.addAll(weeklySummary.weekSummaryList);

			final List<String> titleList = new ArrayList<String>();
			for (WeekSummary w : weekSummaryList) {
				titleList.add(w.getTitle());
			}
			tabs.setValueInfo(titleList);
		}

		notifyDataSetChanged();
	}

	private void initExerciseSummaryInfo() {
		weeklySummary = WeeklySummary.getInstance();
	}

	private void notifyDataSetChanged() {
		if(selectedIndex >= 0 && selectedIndex <  weekSummaryList.size()){
			
	
			WeekSummary selectedWeek = weekSummaryList.get(selectedIndex);
	
			DailySummary todaySummary = App.i().getDailySummary();
			int todayIndex = FormatUtil.calcIdxOfWeek(todaySummary.getDate(), C.exercise.record_date_pattern) - 1;
			if(weekSummaryList.size() -1 == selectedIndex) {
				selectedWeek.dailySummaryList.set(todayIndex, todaySummary); //重设今天
			}
			viewChart.setValueInfo(selectedWeek.dailySummaryList);
		}
	}

	private OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {
		@Override public void onItemSelected(int position) {
			selectedIndex = position;
			notifyDataSetChanged();
		}
	};

	private void registerRingMessageReceiver() {
		if (holderActivity != null) {
			IntentFilter filter = new IntentFilter();
			filter.addAction(RingBleService.ACTION_GATT_EXERCISE_INFO_REPORT);
			holderActivity.registerReceiver(ringMessageReceiver, filter);
		}
	}

	private void unregisterRingMessageReceiver() {
		if (holderActivity != null) {
			holderActivity.unregisterReceiver(ringMessageReceiver);
		}
	}

	private final BroadcastReceiver ringMessageReceiver = new BroadcastReceiver() {
		@Override public void onReceive(Context context, Intent intent) {
			final String action = intent.getAction();
			if (RingBleService.ACTION_GATT_EXERCISE_INFO_REPORT.equals(action)) {
				notifyDataSetChanged();
			}
		}
	};

}
